﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Wintellect.Sterling;
using System.Collections;

namespace TM.QWeibo.Cache
{
    public class SterlingService
    {
        private static ISterlingDatabaseInstance _database = null;
        private static SterlingEngine _engine = null;
        //private static SterlingDefaultLogger _logger = null;

        /// <summary>
        /// 数据库实例
        /// </summary>
        public static ISterlingDatabaseInstance DataBase {
            get 
            {
                return _database;
            }
        }

        /// <summary>
        /// 激活
        /// </summary>
        public static void Active() {
            try
            {
                _engine = new SterlingEngine();
                //_logger = new SterlingDefaultLogger(SterlingLogLevel.Error);
                _engine.Activate();
                _database = _engine.SterlingDatabase.RegisterDatabase<ItemDatabase>();
            }
            catch{
                
            }
        }
        /// <summary>
        /// 消除
        /// </summary>
        public static void Deactivate()
        {
            try
            {
                //_logger.Detach();
                _engine.Dispose();
                _database = null;
                _engine = null;
            }
            catch
            {
                
            }
        }

        private static void Save(IEnumerable list) {
            foreach (var obj in list)
            {
                DataBase.Save(obj);
            }
            DataBase.Flush();
        }

        public static void Purge()
        {
            var tweets = (from o in DataBase.Query<SDK.Objects.Tweet, string>() select o.LazyValue.Value).ToList();
            tweets.ForEach(o => DataBase.Delete(o));
            DataBase.Flush();
        }

        public static void Clear()
        {
            DataBase.Purge();
        }
    }
}
